<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>本科授课管理</span>
        <div class="extra">
          <el-button type="primary" @click="confirmAutoFill">word自助填写</el-button>
          <el-button type="primary" @click="visibleDrawer = true">添加本科授课信息</el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="学期：">
        <el-select placeholder="请选择" v-model="semester">
          <el-option label="第一学期" value="1"></el-option>
          <el-option label="第二学期" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getUndergraduateInstructions">搜索</el-button>
        <el-button @click="semester = null">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 本科授课列表 -->
    <el-table :data="formattedUndergraduateInstructions" style="width: 100%">
      <el-table-column label="课程名称" prop="courseName"></el-table-column>
      <el-table-column label="班级名称" prop="className"></el-table-column>
      <el-table-column label="授课时间（小时）" prop="classHours"></el-table-column>
      <el-table-column label="学生人数" prop="students"></el-table-column>
      <el-table-column label="学期" prop="semester"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="editUndergraduateInstruction(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteUndergraduateInstruction(row.id)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 20, 30]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉 -->
    <el-drawer
      v-model="visibleDrawer"
      title="添加本科授课信息"
      direction="rtl"
      size="50%"
    >
      <el-form :model="undergraduateInstructionModel" label-width="100px">
        <el-form-item label="课程名称">
          <el-input v-model="undergraduateInstructionModel.courseName" placeholder="请输入课程名称"></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="undergraduateInstructionModel.className" placeholder="请输入班级名称"></el-input>
        </el-form-item>
        <el-form-item label="授课时间（小时）">
          <el-input v-model="undergraduateInstructionModel.classHours" placeholder="请输入授课时间"></el-input>
        </el-form-item>
        <el-form-item label="学生人数">
          <el-input v-model="undergraduateInstructionModel.students" placeholder="请输入学生人数"></el-input>
        </el-form-item>
        <el-form-item label="学期">
          <el-select v-model="undergraduateInstructionModel.semester" placeholder="请选择学期">
            <el-option label="第一学期" value="1"></el-option>
            <el-option label="第二学期" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateUndergraduateInstruction">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { Edit, Delete } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  undergraduateInstructionListService,
  undergraduateInstructionAddService,
  undergraduateInstructionUpdateService,
  undergraduateInstructionDeleteService,
} from '@/api/undergraduateInstruction';

// 数据模型
const undergraduateInstructions = ref([]);
const undergraduateInstructionModel = ref({
  id: null,
  courseName: '',
  className: '',
  classHours: '',
  students: '',
  semester: '',
});
const visibleDrawer = ref(false);
const semester = ref(null);
const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 计算属性：格式化后的本科授课列表
const formattedUndergraduateInstructions = computed(() => {
  return undergraduateInstructions.value.map(instruction => ({
    ...instruction,
    semester: instruction.semester === 1 ? '第一学期' : '第二学期',
  }));
});

// 获取本科授课列表
const getUndergraduateInstructions = async () => {
  const params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    semester: semester.value,
  };
  const { data } = await undergraduateInstructionListService(params);
  undergraduateInstructions.value = data;
  total.value = data.length;
};

// 添加或更新本科授课
const addOrUpdateUndergraduateInstruction = async () => {
  if (undergraduateInstructionModel.value.id) {
    await undergraduateInstructionUpdateService(undergraduateInstructionModel.value);
    ElMessage.success('更新成功');
  } else {
    await undergraduateInstructionAddService(undergraduateInstructionModel.value);
    ElMessage.success('添加成功');
  }
  visibleDrawer.value = false;
  getUndergraduateInstructions();
};

// 删除本科授课
const deleteUndergraduateInstruction = async (id) => {
  await ElMessageBox.confirm('确认删除该本科授课信息吗？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  });
  await undergraduateInstructionDeleteService(id);
  ElMessage.success('删除成功');
  getUndergraduateInstructions();
};

// 编辑本科授课
const editUndergraduateInstruction = (row) => {
  undergraduateInstructionModel.value = {
    ...row,
    semester: row.semester === '第一学期' ? 1 : 2, // 将显示的学期转换回数值
  };
  visibleDrawer.value = true;
};

// 分页变化
const onSizeChange = (size) => {
  pageSize.value = size;
  getUndergraduateInstructions();
};
const onCurrentChange = (num) => {
  pageNum.value = num;
  getUndergraduateInstructions();
};
// 确认自助填写
const confirmAutoFill = () => {
  ElMessageBox.confirm('是否自助填写word文档？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'info',
  }).then(() => {
    ElMessage.success('自助填写已确认');
    // 在此处添加自助填写的逻辑
  }).catch(() => {
    ElMessage.info('自助填写已取消');
  });
};

onMounted(() => {
  getUndergraduateInstructions();
});
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
